package storage

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3" // 操作引擎初始化
	"log"
)

// 进行文件存储操作
type msg struct {
	id    int
	name  string
	usage string
}

const dataSource = "/home/hufeng/GolandProjects/hufeng_review/hufeng.sqlite"

// 暂时使用一个sqlite，后续更新问自研存储数据库
func link() {
	db, err := sql.Open("sqlite3", dataSource)
	if err != nil {
		log.Println(err)
		return
	}
	fmt.Println("success link to sqlite")
	defer func() {
		if err = db.Close(); err != nil {
			log.Println(err)
			return
		}
	}()
	sqlStmt := `CREATE TABLE IF NOT EXISTS hufeng_review (ID INTEGER PRIMARY KEY not null,NAME TEXT NOT NULL, USAGE TEXT NOT NULL);` // Stmt是statement的缩写，指的是SQL（结构化查询语言）语句
	msg0 := &msg{
		id:    0,
		name:  "webLink",
		usage: "用于客户端/服务端进行双向通信",
	}

	err = CreateTable(sqlStmt, db)
	if err != nil {
		log.Println(err)
		return
	}
	//err = AddTableLine(msg0, db)
	if err != nil {
		err = fmt.Errorf("error on add msg to db: %v", err)
		if err != nil {
			panic(err)
		}
	}
	err = DeleteTableLine("hufeng_review", 2, "ArticleID")
	msg0.usage = "长连接以及后台通知，推送消息布置等工作"
	err = UpdateTableLine("hufeng_review", msg0)
	tableLink, err := GetTableLink("hufeng_review")
	if err != nil {
		log.Println(err)
		return
	}
	fmt.Println(tableLink)
}
